<?php


$config["dbsetup"][]="IncProcessAllDatabase"; // register a required database checkup

$config["xAPInc"][]=array("class"=>"all","function"=>"IncProcessMsg"); // register receiver



function IncProcessMsg($msg){
		foreach($msg["body"] as $Key=>$Value){
			foreach ($Value as $sKey=>$sValue){
				IncProcessItem($msg["header"]["uid"],$msg["header"]["source"],$msg["header"]["class"],$Key,$sKey,$sValue);
			}
		}
}
function IncProcessItem($uid,$source,$class,$section,$item,$value){
//get current state
	$sql="select xValue,DeviceID,Log from  xAP_device where 
		source='".dbesc(strtolower($source))."' and 
		class='".dbesc(strtolower($class))."' and 
		section='".dbesc(strtolower($section))."' and 
		uid='".dbesc(strtoupper($uid))."' and 
		xitem='".dbesc(strtolower($item))."' and
		xType=1";
	$results=dbquery($sql);
	if(isset($results[0]["xValue"])){
		$sql="update xAP_device set xValue='".dbesc($value)."', Updated='".date("Y-m-d H:i:s")."' where 
			DeviceID='".dbesc($results[0]["DeviceID"])."'";
			dbexec($sql);
			xAPLog("xAP","UPDATE ".$source,"$class:$section:$item:$value");
	}
	else
	{
		$sourcemain=$source;
		$sourcesub="";
		if(strpos($source,":")>0){
			$sourcemain=substr($source,0,strpos($source,":"));
			$sourcesub=substr($source,strpos($source,":")+1);
		}
	
		$sql="insert into xAP_Device (DeviceID,UID,Source,SourceMain,SourceSub,Class,Section,xItem,xValue,Log,Created,Updated,xType) VALUES (
		Null,
		'".dbesc(strtoupper($uid))."',
		'".dbesc(strtolower($source))."',
		'".dbesc(($sourcemain))."',
		'".dbesc(($sourcesub))."',
		'".dbesc(strtolower($class))."',
		'".dbesc(strtolower($section))."',
		'".dbesc(strtolower($item))."',
		'".dbesc(($value))."',
		0,
		'".date("Y-m-d H:i:s")."',
		'".date("Y-m-d H:i:s")."',
		1
		)";
		dbexec($sql);
		xAPLog("xAP","INSERT ".$source,"$class:$section:$item:$value");
		//defaults
		$results[0]["Log"]=0;
	}
	// do things on change
	if($results[0]["xValue"]!=$value){
	// log message?
		if($results[0]["Log"]==1){
			$sql="insert into xAP_Device_Log (LogID,DeviceID,xValue,Created) VALUES (
			Null,
			'".dbesc($results[0]["DeviceID"])."',
			'".dbesc($value)."',
			'".date("Y-m-d H:i:s")."'
			)";
			dbexec($sql);
			xAPLog("xAP","LOG ".$source,"$class:$section:$item:$value");
		}
	}
}
function IncProcessAllDatabase(){
	// device table
	dbexec("CREATE TABLE IF NOT EXISTS xAP_Device (DeviceID INTEGER  AUTO_INCREMENT PRIMARY KEY)");
	dbexec("ALTER TABLE xAP_Device add xType INTEGER");  // 1 raw, 2 BSC, 3 TSC
	dbexec("ALTER TABLE xAP_Device add Alias VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Device add Source VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Device add SourceMain VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Device add SourceSub VARCHAR(255)");
	dbexec("ALTER TABLE xAP_Device add UID VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Device add Class VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Device add Section VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Device add xItem VARCHAR(50)");
	dbexec("ALTER TABLE xAP_Device add xValue VARCHAR(1000)");//bsc state
	dbexec("ALTER TABLE xAP_Device add xValue2 VARCHAR(20)"); //bsc level
	dbexec("ALTER TABLE xAP_Device add xValue3 VARCHAR(20)"); //bsc levelmax
	dbexec("ALTER TABLE xAP_Device add xValue4 VARCHAR(255)"); //bsctext
	dbexec("ALTER TABLE xAP_Device add xValue5 VARCHAR(255)");//bsc display text
	dbexec("ALTER TABLE xAP_Device add Created datetime");
	dbexec("ALTER TABLE xAP_Device add Updated datetime");
	dbexec("ALTER TABLE xAP_Device add Log INTEGER");

	dbexec("CREATE TABLE IF NOT EXISTS xAP_Device_Log (LogID INTEGER  AUTO_INCREMENT PRIMARY KEY)");
	dbexec("ALTER TABLE xAP_Device_Log add DeviceID INTEGER");
	dbexec("ALTER TABLE xAP_Device_Log add xValue VARCHAR(1000)");
	dbexec("ALTER TABLE xAP_Device_Log add xValue2 VARCHAR(20)"); //bsc level
	dbexec("ALTER TABLE xAP_Device_Log add xValue3 VARCHAR(20)"); //bsc levelmax
	dbexec("ALTER TABLE xAP_Device_Log add xValue4 VARCHAR(255)"); //bsctext
	dbexec("ALTER TABLE xAP_Device_Log add xValue5 VARCHAR(255)");//bsc display text
	dbexec("ALTER TABLE xAP_Device_Log add Created datetime");


}
?>